<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MarkItUp.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="Wikey.Domain.Components"%>

<asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server">

  <script type="text/javascript">
    $(document).ready(function Init() {
      mySettings.onSave = function(markItUp) {
        data = markItUp.textarea.value;
        ok = confirm("Save the content?");
        if (!ok) {
          return false;
        }
        post = { Name: $('#PageTitle').val(), Parent: $("#ParentPage").val(), Tags: GetTags(), Description:$("#Description").val(), Space: $('#SpaceId').val(), data: data, documents: $("#PageDocuments").val() };
        $.post('<%= Html.ResolveUrl("~/Page/Create")%>',
                post,
                function(response) {
                  var obj = eval('(' + response + ')');
                  $("#successfull").text("Page saved successfully with id " + obj.Id);
                  $("#successfull").show();
                  $("#successfull").modal({ onOpen: function(dialog) {
                    dialog.overlay.fadeIn('slow', function() {
                      dialog.data.hide();
                      dialog.container.fadeIn('slow', function() {
                        dialog.data.slideDown('slow');
                      });
                    });
                  }, onClose: function(dialog) {

                    dialog.data.fadeOut('slow', function() {
                      dialog.container.hide('slow', function() {
                        dialog.overlay.slideUp('slow', function() {
                          $.modal.close();
                          $("#successfull").hide();
                          document.location = serverBase + "Pages/" + obj.Id;
                        });
                      });
                    });

                  }
                  }
                  );

                  //alert(response);
                }
            );
        function GetTags() {
          var text = '';
          $(".tagEditor").find("li").each(function(ref, data) { text += data.innerHTML + ","; })

          return text;
        }
      };

      $('#MarkItText').markItUp(mySettings);
    }
  );
    $(function() {
      var btnUpload = $('#upload');
      var status = $('#status');
      new AjaxUpload(btnUpload, {
        action: '<%= Html.ResolveUrl("~/Page/Upload")%>',
        //Name of the file input box  
        name: 'uploadfile',
        onSubmit: function(file, ext) {
          if (!(ext && /^(jpg|png|jpeg|gif|doc|xls|xml|xlsx|docx|txt|pdf|zip|rar)$/.test(ext))) {
            // check for valid file extension  
            status.text('Only JPG, PNG or GIF files are allowed');
            return false;
          }
          status.text('Uploading...');
        },
        onComplete: function(file, response) {
          //On completion clear the status  
          status.text('');
          jsonResponse = response.replace("<pre>", "").replace("</pre>", "");

          jsonObj = eval('(' + jsonResponse + ')');

          try {
            for (var i = 0; i < jsonObj.length; i++) {
              $('<li></li>').appendTo('#files').html('<img src="./Uploads/' + file + '" alt="" /><br />' + file).addClass('success');
              $("#PageDocuments").val($("#PageDocuments").val() + "|" + jsonObj[i].Id);

            }
          } catch (e) {
            $('<li></li>').appendTo('#files').text(file).addClass('error');

          }

        }
      });     
    });
    
  </script>

  <div style="float:left;width: 70%">
    <h2>
      Create Page in 
      <% Html.RenderPartial("PageSpace");%>
      
    </h2>
    
    <div id="MarkupDiv">
      <label for="PageTitle" style='width:200px'>Title</label>
      <input type="text" style="width: 500px; height: 30px; font-size: 20px; color: Blue;"
        id='PageTitle' />
        <label for="Description" style='width:200px'>Description of your page:</label>
       <textarea id="Description" cols="50" rows="5"></textarea>
      <textarea id="MarkItText" cols="200" rows="100"></textarea>
    </div>

  </div>
  
  <div style="float: left; width: 25%">
    <% Html.RenderPartial("PageSettings"); %>
        <!-- Upload Button-->
    <div id="upload" style="cursor:pointer;">
      Upload File</div>
    <span id="status"></span>
    <!--List Files-->
    <ul id="files">
    </ul>
    <input type="hidden" id="PageDocuments" />
    <input type="hidden" id="ParentPage" value="<%= ViewData["Parent"] %>" />
  </div>
  <div id="successfull" style="display:none">
    Page save completed successfully.
  </div>
  <div style="clear:both"></div>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
</asp:Content>
